import { Tsrv } from '@/services';

/**
 * 设置二级参数
 * @param dict
 * @param level1
 * @param level2
 * @param value
 */
const setLevelParams = (
  dict: any,
  level1: string,
  level2: string,
  value: any
) => {
  if (dict && dict[level1] !== undefined) {
    if (dict[level1][level2] !== undefined) {
      dict[level1][level2] = value;
      return true;
    }
  }
  return false;
};

/**
 * 增加field value格式
 * @param array
 * @param field
 * @param value
 */
const pushFieldValueDict = (
  array: any,
  field:
    | string
    | { field: string; value: string | number | null }
    | { field: string; value: string | number | null }[],

  value: string | number | null = null
) => {
  // eslint-disable-next-line no-shadow
  const add = (field: string, value: string | number | null) => {
    const find = Tsrv.utils.find(array, el => el.field === field);
    if (find) {
      if (value !== '' && value !== null) {
        find.value = Tsrv.utils.toString(value);
      } else {
        Tsrv.utils.remove(array, el => el.field === field);
      }
    } else {
      if (value !== '' && value !== null) {
        array.push({
          field,
          value: Tsrv.utils.toString(value)
        });
      }
    }
  };
  if (Tsrv.utils.isString(field)) {
    add(field as string, value);
  } else if (Tsrv.utils.isPlainObject(field)) {
    field = Tsrv.utils.clone(field, true) as {
      field: string;
      value: string | number | null;
    };
    add(field.field, field.value);
  } else if (Tsrv.utils.isArray(field)) {
    field = Tsrv.utils.clone(field, true) as {
      field: string;
      value: string | number | null;
    }[];
    field.forEach(el => {
      add(el.field, el.value);
    });
  }
};

/**
 * 移除field value格式
 * @param array
 * @param field
 */

const removeFieldValueDict = (array: any, field: string | string[]) => {
  // eslint-disable-next-line no-shadow
  const remove = (field: string) => {
    Tsrv.utils.remove(array, el => el.field === field);
  };
  if (Tsrv.utils.isString(field)) {
    remove(field as string);
  } else if (Tsrv.utils.isArray(field)) {
    field = field as string[];
    field.forEach(el => {
      remove(el);
    });
  }
};

/**
 * 数字格式化
 * @param cellValue
 * @param format
 */
const numberFormatter = (cellValue, format) => {
  let res = cellValue;
  if (format) {
    if (format.type !== '') {
      switch (format.type) {
        case 'number':
          if (format.thousandmark) {
            // res = Tsrv.utils.toFixedString(cellValue, format.decimal || 2);
            res = Number(cellValue).toFixed(format.decimal || 2);
            res = Tsrv.utils.commafy(res);
          } else {
            // res = Tsrv.utils.toFixedString(cellValue, format.decimal || 2);
            res = Number(cellValue).toFixed(format.decimal || 2);
          }
          break;
        case 'currency':
          res = Tsrv.utils.toFixedString(cellValue, format.decimal || 2);
          res = format.currency + Tsrv.utils.commafy(res);
          break;
        case 'percent':
          res = Tsrv.utils.toString(Tsrv.utils.toNumber(cellValue) * 100);
          res = `${Tsrv.utils.toFixedString(res, format.decimal || 2)}%`;
          break;

        default:
          break;
      }
    }
  }
  return res === '' ? null : res;
};

/**
 * 时间格式化
 * @param value
 * @param format
 * @param type
 */
const timeFormatter = (value, format, type) => {
  if (value && value !== null) {
    if (type === 'time') {
      // return Tsrv.utils.getTime(`1970-01-01 ${value}`, format);
      return Tsrv.utils.toDateString(`1970-01-01 ${value}`, format);
    }
    if (type === 'timerange') {
      const arr = value.split(' - ');
      // return `${Tsrv.utils.getTime(
      //   `1970-01-01 ${arr[0]}`,
      //   format
      // )} - ${Tsrv.utils.getTime(`1970-01-01 ${arr[1]}`, format)}`;
      return `${Tsrv.utils.toDateString(
        `1970-01-01 ${arr[0]}`,
        format
      )} - ${Tsrv.utils.toDateString(`1970-01-01 ${arr[1]}`, format)}`;
    }
    if (type === 'date' || type === 'datetime') {
      return Tsrv.utils.toDateString(value, format);
    }
  }
  return null;
};

export default {
  setLevelParams,
  pushFieldValueDict,
  removeFieldValueDict,
  numberFormatter,
  timeFormatter
};
